package com.luych.toolbox.base.service.service;

import com.luych.toolbox.base.service.constant.Constant;
import com.luych.toolbox.base.service.dao.TaskLogDao;
import com.luych.toolbox.base.service.entity.TaskLog;
import com.luych.toolbox.common.event.base.service.publisher.WebSocketTaskPublisher;
import com.luych.toolbox.common.feign.param.base.service.TaskLogParam;
import com.luych.toolbox.common.feign.param.gateway.center.WebSocketTask;
import com.luych.toolbox.common.feign.view.base.service.TaskLogView;
import com.luych.toolbox.common.service.base.BaseConvert;
import com.luych.toolbox.common.service.base.BaseService;
import com.luych.toolbox.common.service.exception.HttpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

@Service
public class TaskLogService extends BaseService<TaskLogView, TaskLogParam, TaskLog> {

    private static final BaseConvert<TaskLogParam, TaskLog> taskLogParamToEntity = TaskLog::new;
    private static final BaseConvert<TaskLog, TaskLogView> taskLogEntityToView = TaskLogView::new;

    private final WebSocketTaskPublisher taskPublisher;

    @Autowired
    public TaskLogService(TaskLogDao taskDao, WebSocketTaskPublisher taskPublisher) {
        super(taskDao, taskLogParamToEntity, taskLogEntityToView);
        this.taskPublisher = taskPublisher;
    }

    @Override
    public TaskLogView create(TaskLogParam param) {
        TaskLogView taskView = super.create(param);
        taskPublisher.send(new WebSocketTask(WebSocketTask.Type.LOG, taskView));
        return taskView;
    }

    @Override
    public TaskLogView modify(TaskLogParam param) {
        throw new HttpException(HttpStatus.BAD_REQUEST, Constant.TASK_LOG_MODIFY_UNSUPPORTED);
    }

    @Override
    public void remove(String id) {
        throw new HttpException(HttpStatus.BAD_REQUEST, Constant.TASK_LOG_REMOVE_UNSUPPORTED);
    }
}
